﻿#region Copyright and Licensing Notice
// Copyright (C) 2011 Steven Bosnick
// 
// This program is open source software; you can redistribute it 
// and/or modifiy it under the terms of the Eclipse Public License,
// version 1.0.
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A 
// PARTICULAR PURPOSE. See the Eclipse Public License for more
// details.
// 
// You should have received a copy of the Eclipse Public License 
// along with this program; if not, you can find a copy at
// "http://www.opensource.org/licenses/eclipse-1.0.php".
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Autofac;
using Autofac.Builder;
using Autofac.Core;
using Autofac.Features.Scanning;
using BitwiseOre.SharedMemory.Core.Common;
using BitwiseOre.SharedMemory.JetBrains;
using BitwiseOre.SharedMemory.Utils;

namespace BitwiseOre.SharedMemory.Autofac
{
    internal static class RegistrationExtensions
    {
        [UsedImplicitly]
        public static IRegistrationBuilder<TLimit,TScanningActivatorData,TRegistrationStyle> 
            AsSharedInterfaces<TLimit, TScanningActivatorData, TRegistrationStyle>(
                this IRegistrationBuilder<TLimit,TScanningActivatorData,TRegistrationStyle> registration) 
            where TScanningActivatorData : ScanningActivatorData
        {
            return registration.As(
                (Func<Type, IEnumerable<Service>>) (t => from @interface in t.GetInterfaces()
                                                         where @interface != typeof (ISharedObjectInit)
                                                         select new TypedService(@interface)));
        }

        private static IRegistrationBuilder<TLimit, ConcreteReflectionActivatorData, SingleRegistrationStyle>
            KeyedAsImplementedInterfaces<TLimit>(
            this IRegistrationBuilder<TLimit, ConcreteReflectionActivatorData, SingleRegistrationStyle> registration,
            object key)
        {
            var services = from @interface in typeof (TLimit).GetInterfaces()
                           select new KeyedService(key, @interface);
            return registration.As(services.ToArray());
        }

        public static void RegisterRoot<TRoot,TConfig>(
            this ContainerBuilder builder)
        {
            builder.RegisterType<TConfig>()
                .InstancePerLifetimeScope()
                .KeyedAsImplementedInterfaces(typeof(TConfig))
                .AsSelf();

            builder.RegisterType<TRoot>()
                .ExternallyOwned()
                .FindConstructorsWith(BindingFlags.NonPublic)
                .WithParameter((pi, c) => pi.ParameterType == typeof (IDisposer<TRoot>),
                               (pi, c) => new Disposer<TRoot>(c.Resolve<ILifetimeScope>()))
                .WithMetadata(MetadataConstants.RootConfigKey, typeof (TConfig));

        }
    }
}